package com.unytech.project.entity.system;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Index;

import com.unytech.project.entity.BaseEntity;

/**
 * 用户实体类
 * @author DolphinBoy
 *
 */
@Entity 
@Table(name="T_USER")
@GenericGenerator(name="uuidpk",strategy="uuid")  //这个是Hibernate的主键生成器，换框架的时候，此处应注意
//@DiscriminatorValue("0")  //鉴别器的值
//@Cacheable(value=true)				//这个是Hibernate的缓存注解
public class User extends BaseEntity {
	@Id @GeneratedValue(generator="uuidpk")
	@Column(length=32, nullable=false)
	private String userid;                //用户ID
	
	@Column(length=32,nullable=false,unique=true)
	@Index(name="username_index")        //这个是Hibernate的注解，换框架的时候，此处应注意
	private String username;             //登录账号
	
//	@Column(length=50,nullable=true)
//	private String realname;
	
	@Column(length=32,nullable=true)
	private String password;              //密码， MD5加密	
	
	/** 账户状态，默认启用 */
	private Integer state = 1;            
	
	@Temporal(TemporalType.TIMESTAMP) 
	@Column(nullable=false)
	private Date regdate = new Date();    //用户写入时间
	
	
	/** 依赖的人员 */
	//从业务上来讲对于一个用户来说是必须有一个人员来对应的，所以optional应该设为false，但是考虑到超级管理员的特殊性，
	//这里就改为true，必须性由代码来控制而不是数据库来约束
	@OneToOne(cascade=CascadeType.REFRESH, optional=true)  
	@JoinColumn(name="person_id")
	private Person person;
	
	@ManyToOne(cascade={CascadeType.REFRESH}, optional=true)  //每个账户需要一个创建人，这里创建人设置为可以为空
	@JoinColumn(name="createperson_id")      //外键列名
	private User createperson;            //账户创建人
	
	@ManyToOne(cascade={CascadeType.REFRESH}) 
	@JoinColumn(name="permgroup_id")      //外键列名
	private PermissionGroup permgroup;    //用户所属权限组
	
	/** 用户登录记录 */
//	@OneToMany(cascade={CascadeType.REFRESH,CascadeType.MERGE},fetch=FetchType.LAZY,mappedBy="loginuser")
//	private Set<LoginRecord> loginrecord = new HashSet<LoginRecord>();
	
//	@OneToMany(cascade={CascadeType.REFRESH,CascadeType.MERGE},fetch=FetchType.LAZY,mappedBy="recorduser")
//	private Set<OperateRecord> operaterecord = new HashSet<OperateRecord>();
//	
	//用户登录信息，备用域
	/** 用户登录IP */
	@Transient
	private String loginip;           
	
	/** 用户登录时间 */
	@Transient     
	private Date logintime;           

	public String getUserid() {
		return userid;
	}
	public void setUserid(String userid) {
		this.userid = userid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Integer getState() {
		return state;
	}
	public void setState(Integer state) {
		this.state = state;
	}
	public PermissionGroup getPermgroup() {
		return permgroup;
	}
	public void setPermgroup(PermissionGroup permgroup) {
		this.permgroup = permgroup;
	}
	public void setRegdate(Date regdate) {
		this.regdate = regdate;
	}
	public Date getRegdate() {
		return regdate;
	}
//	public void setRealname(String realname) {
//		this.realname = realname;
//	}
//	public String getRealname() {
//		return realname;
//	}
	public void setCreateperson(User createperson) {
		this.createperson = createperson;
	}
	public User getCreateperson() {
		return createperson;
	}
	public void setLogintime(Date logintime) {
		this.logintime = logintime;
	}
	public Date getLogintime() {
		return logintime;
	}
	public void setLoginip(String loginip) {
		this.loginip = loginip;
	}
	public String getLoginip() {
		return loginip;
	}
//	public Set<OperateRecord> getOperaterecord() {
//		return operaterecord;
//	}
//	public void setOperaterecord(Set<OperateRecord> operaterecord) {
//		this.operaterecord = operaterecord;
//	}
//	public Set<LoginRecord> getLoginrecord() {
//		return loginrecord;
//	}
//	public void setLoginrecord(Set<LoginRecord> loginrecord) {
//		this.loginrecord = loginrecord;
//	}
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	  
}
